package com.jwb.pub.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.service.PrivilegeService;
import com.jwb.util.MainController;



/**
 * 
 * @description:功能权限管理PrivilegeController
 * @author liuyn
 *
 * @createDate 2018-03-15
 * @createTime 上午11:22:45
 */
@Slf4j
@Controller
@RequestMapping(value="/pub/privilege" )
public class PrivilegeController extends MainController{


	@Autowired
	private PrivilegeService privilegeService;
	
	
	/**
	 * http://127.0.0.1:8080/services//privilege/privilegeListMenu.mvc
	 * @description：显示权限列表菜单
	 * @author liuyn
	 * @return	
	 * @createDate 2018-3-15
	 * @createTime 下午5:04:33
	 */
    @RequestMapping(value="/privilegeListMenu")
    @ResponseBody
	public String privilegeListMenu(HttpServletRequest req) throws Exception
	{
    	JSONObject jsobject = new JSONObject();	
    	JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
    	jsobject=privilegeService.getPGroupsJson(jsdata);
    	log.info("privilegeListMenu: "+jsobject.toString());
    	return jsobject.toString();
	}
	
    
    
	/**
	 * http://127.0.0.1:8080/services//privilege/privilegeListMain.mvc?groupOrder=1
	 * @description：显示权限列表
	 * @author liuyn
	 * @return	
	 * @createDate 2018-3-15
	 * @createTime 下午5:04:33
	 */
    @RequestMapping(value="/privilegeListMain")
    @ResponseBody
	public String privilegeListMain(HttpServletRequest req) throws Exception
	{
    	JSONObject jsobject = new JSONObject();	
//    	String sqlWhere="";
//    	String groupOrder=req.getParameter("groupOrder");
//    	sqlWhere=" and group_order='"+groupOrder+"'";
    	JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
    	jsobject=privilegeService.getPrivListJson(jsdata);
    	log.info("privilegeListMain: "+jsobject.toString());
    	return jsobject.toString();
	}
    
    
    /**
     * http://127.0.0.1:8080/services//privilege/privilegeFunSave.mvc?privCode=17&openSts=1
     * @description：保存功能开关状态
	 * @author liuchang
	 * 
	 * @return	
	 * @createDate 2018-3-15
	 * @createTime 下午14:34
     * 
     */

    @RequestMapping("/privilegeFunSave")
    @ResponseBody
    public String privilegeFunSave(HttpServletRequest req){
    	JSONObject jsobject = new JSONObject();
//    	String privCode = req.getParameter("privCode");
//    	String openSts=req.getParameter("openSts");
//    	String sqlWhere= " priv_code = '"+privCode+"'";
    	JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
    	jsobject = privilegeService.privilegeStsSave(jsdata);
    	
		log.info("privilegeFunSave():" + jsobject.toString());

    	return jsobject.toString();
    }
    
    
    /**
	 * 
	 * @description：显示权限详细信息
	 * @author LIU CHANG
	 * 
	 * @return
	 * @throws DBException
	 * 
	 * @createDate 2014-3-7
	 * @createTime 下午5:11:34
	 */
    @RequestMapping("/privilegeView")
    @ResponseBody
    public String privilegeView(HttpServletRequest req) throws Exception{
    	JSONObject jsobject = new JSONObject();
//    	String privCode = req.getParameter("privCode");
//    	String sqlWhere = " priv_code = '"+privCode+"'";
    	JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
    	jsobject = privilegeService.getPrivJson(jsdata);
    	
    	return jsobject.toString();
    }
    
    
    
    /**
	 * 
	 * @description：添加权限信息
	 * @author LIU CHANG
	 * 
	 * @return
	 * @throws DBException
	 * 
	 * @createDate 2014-3-7
	 * @createTime 下午5:12:54
	 */
    
    @RequestMapping("/privilegeAdd")
    @ResponseBody
	public String privilegeAdd(HttpServletRequest req) throws Exception{
		JSONObject jsobject = new JSONObject();
		//String groupOrder = req.getParameter("groupOrder");
		JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
		jsobject = privilegeService.getPrivilegeGroupInfo(jsdata);
		String respCode = jsobject.getString("respCode");
		try{
			if(respCode.equals("0")){			
				//respCode\":\"0\",\"respmsg\":\"权限所属组信息显示成功\"
				jsobject.put("pglist", privilegeService.getPGroupsJson(new JSONObject()));
				jsobject.put("respCode", "0");
				jsobject.put("respmsg", "权限所属组信息显示成功");
				
			}else{
				jsobject.put("respCode", "1");
				jsobject.put("respmsg", "权限所属组信息显示失败，请重试!");
			}		
		}
		catch(Exception e){
			jsobject.put("respCode", "9");
			jsobject.put("respmsg", "系统忙，请联系系统管理员!");
			return jsobject.toString();
		}
		
		return jsobject.toString();
	}
    
    
    
    
    /**
	 *  http://localhost:8080/services/privilege/privilegeSave?privCode=17&privName=data_ft_set&remarks=不分摊设置&groupOrder=3&privGroup=数据处理
	 * @description：保存权限信息
	 * @author LIUCHANG
	 * 
	 * @return
	 * @throws DBException
	 * 
	 * @createDate 2014-3-7
	 * @createTime 下午5:15:06
	 */    
    @RequestMapping("/privilegeSave")
    @ResponseBody
    public String privilegeSave(HttpServletRequest req){
//    	String privCode = req.getParameter("privCode");
//    	String privName = req.getParameter("privName");
//    	String remarks = req.getParameter("remarks");
//    	String groupOrder = req.getParameter("groupOrder");
//    	String privGroup = req.getParameter("privGroup");
    	
    	JSONObject jsobject = new JSONObject();
    	JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
    	jsobject = privilegeService.privilegeSave(jsdata);
    	log.info("privilegeSave():" + jsobject.toString());
    	
    	return jsobject.toString();
    }
    
   
    
    /**
	 * 
	 * @description：删除权限信息
	 * @author LIU CHANG
	 * 
	 * @return
	 * 
	 * @createDate 2018-3-16
	 * @createTime 下午4:59:44
	 */
    @RequestMapping("privilegeDel")
    @ResponseBody
	public String privilegeDel(HttpServletRequest req){
    	//String privCode = req.getParameter("privCode");
    	JSONObject jsobject = new JSONObject();
    	JSONObject jsdata = JSONObject.parseObject(req.getParameter("jsondata"));
    	jsobject = privilegeService.delete(jsdata);  
    	
    	log.info("privilegeDel():" + jsobject.toString());
    	return jsobject.toString();
	}
    
    
}
